package com.example.springbootdemo.异步注解失效测试.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;

@Service
@SpringBootTest
@RunWith(SpringRunner.class)
public class PrintOne {
    /**
     * 同类中自动装配，编译报错
     */
//    @Resource
//    PrintOne printOne;
    @Async("asyncExecutors")
    public void java() {
        System.out.println("-----------------------------------------------------elegant separator-----------------------------------------------------------------");
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("Async++++++++子线程java名："+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i + "hello java");
        }
        System.out.println("-----------------------------------------------------elegant separator-----------------------------------------------------------------");
    }
    @Async("asyncExecutors")
    public void python() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println("Async++++++++子线程python名："+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i + "hello python");
        }
    }

    /**
     * 同类中调用异步方法，异步失效
     */
    @Test
    public void getJava() {
        java();
        System.out.println("异步任务执行成功了吗");
    }

    /**
     * 通过new 类调用方法，异步任务失效
     */
    @Test
    public void getPython() {

        PrintOne printOne = new PrintOne();
        printOne.python();
        System.out.println("异步任务执行成功了吗");
    }

}
